﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Ones.Infrastructure.Utils.Pagination.Impl;
using Ones.ServiceAdvertisementManagement;

namespace Ones.WebAdmin
{
    public partial class AdsListAdsPage : System.Web.UI.Page
    {
        private ServiceAdvertisementManagementClient c = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadAdsPaging();
            }
        }

        public void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Session.Add("cmd", e.CommandName);
            Session.Add("id", e.CommandArgument);

            if (e.CommandName.ToString().Equals("detail"))
            {
                Response.Redirect("~/WebAdmin/AdsDetailAdsPage.aspx");
            }

            Response.Redirect("~/WebAdmin/AdsMgmtAdsPage.aspx");
        }

        private void loadAds()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");

            rpt.DataSource = c.searchAllAdvertisementDetails();
            rpt.DataBind();

            c.Close();
        }

        private void loadAdsPaging()
        {
            c = new ServiceAdvertisementManagementClient("adsBasicHttpBinding");
            var lst = c.searchAllAdvertisementDetails();
            
            int pageNumber = 1;
            if (Request.QueryString["p"] != null)
            {
                pageNumber = int.Parse(Request.QueryString["p"]);
            }
            LazyPagination<ViewAdvertisementDetail> lstPaged = new LazyPagination<ViewAdvertisementDetail>(lst.AsQueryable(), pageNumber, 3);

            rpt.DataSource = lstPaged;
            rpt.DataBind();

            pager.PageNumber = lstPaged.PageNumber;
            pager.PageSize = lstPaged.PageSize;
            pager.TotalItems = lstPaged.TotalItems;
            pager.FirstItem = lstPaged.FirstItem;
            pager.LastItem = lstPaged.LastItem;
            pager.HasPreviousPage = lstPaged.HasPreviousPage;
            pager.HasNextPage = lstPaged.HasNextPage;
        }
    }
}